package cn.sumpu.app.guoguojie.biz.impl;

import java.util.*;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import cn.sumpu.app.guoguojie.biz.AccountBiz;
import cn.sumpu.app.guoguojie.biz.AccountDrawApplyBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.constant.TransactionType;
import cn.sumpu.app.guoguojie.dao.AccountDrawApplyDao;
import cn.sumpu.app.guoguojie.domain.AccountDrawApply;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;

public class AccountDrawApplyBizImpl implements AccountDrawApplyBiz {
    
    private final static Logger            logger    = LoggerFactory.getLogger(AccountDrawApplyBizImpl.class);

    @Autowired
    private IdentifierBiz identifierBiz;
    @Autowired
    private AccountBiz accountBiz;
    
    private DataSourceTransactionManager   transactionManager;
    
    private AccountDrawApplyDao applyDao;

    public void setTransactionManager(DataSourceTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }
    
    public void setApplyDao(AccountDrawApplyDao applyDao) {
        this.applyDao = applyDao;
    }

    //添加 会员提现申请
    @Override
    public void addAccountDrawApply(AccountDrawApply apply) throws AppBusinessException {
        apply.setIdentifier(identifierBiz.generateId("account_draw_apply"));
        applyDao.addAccountDrawApply(apply);
    }
    //更新 会员提现申请
    @Override
    public void updateAccountDrawApply(AccountDrawApply apply) throws AppBusinessException {
        applyDao.updateAccountDrawApply(apply);
    }
    //根据ID查询 会员提现申请 信息
    @Override
    public AccountDrawApply getAdaById(String id) throws AppBusinessException {
        return applyDao.getAdaById(id);
    }
    //查询 会员提现申请 列表
    @Override
    public PageModel getAdasByCondition(String nickname, String status, Integer pageSize, Integer page) throws AppBusinessException {
        if (page == null || page < 1)
            page = 1;
        if (pageSize == null || pageSize < 1)
            pageSize = 20;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("nickname", Common.emptyStr(nickname)?null:nickname);
        map.put("status", "00".equals(status)?null:status);
        map.put("pageSize", pageSize);
        map.put("startId", (page - 1) * pageSize);
        List<AccountDrawApply> list = applyDao.getAdasByCondition(map);
        int count = getAdaCountByCondition(nickname, status);
        PageModel pm = new PageModel();
        pm.setCurrentPage(page);
        pm.setList(list);
        pm.setPageSize(pageSize);
        pm.setTotalRecord(count);
        pm.setTotalPage((count + pageSize - 1) / pageSize);
        return pm;
    }

    @Override
    public Integer getAdaCountByCondition(String nickname, String status) throws AppBusinessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("nickname", nickname);
        map.put("status", status);
        return applyDao.getAdaCountByCondition(map);
    }
    //审核会员提现申请
    @Override
    public void auditApply(AccountDrawApply apply) throws AppBusinessException { 
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();// 事务定义类
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);// 设置事务传播属性
        def.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);// 设置事务隔离级别
        def.setTimeout(10);// 设置事务超时时间
        TransactionStatus status = transactionManager.getTransaction(def);// 返回事务对象
        try {
            //更新提现状态
            apply.setStatus("02");
            updateAccountDrawApply(apply);
            //转帐交易
            accountBiz.transferRmbByAccountId(apply.getAccount(), Common.GUOGUOJIE_ACCOUNT_ID, apply.getDrawRmb(), "会员申请提现："+apply.getNickname(), "system", TransactionType.RMB_OP_DEAL);
            transactionManager.commit(status);// 提交事务
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("审核会员提现申请 出错", e);
            }
            transactionManager.rollback(status);// 回滚事务
        }
    }
    
}
